{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ec4c37bd4ee1"
   },
   "source": [
    "# Getting started with IonQ and Cirq on Azure Quantum\n",
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/hardware/azure-quantum/getting_started_ionq\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/main/docs/hardware/azure-quantum/getting_started_ionq.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/main/docs/hardware/azure-quantum/getting_started_ionq.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/hardware/azure-quantum/getting_started_ionq.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7326c6ee6d26"
   },
   "source": [
    "This notebooks shows how to send a basic quantum circuit to an IonQ target via Azure Quantum.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "- To work in Azure Quantum, you need an Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/).\n",
    "- Create an Azure Quantum workspace and enable IonQ. For more information, see [Create an Azure Quantum workspace](https://docs.microsoft.com/azure/quantum/quickstart-microsoft-qc?pivots=platform-ionq#create-an-azure-quantum-workspace)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "d48b381b86ed"
   },
   "source": [
    "First, install `azure-quantum` with the Cirq dependencies:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "96e68f63b1d0"
   },
   "outputs": [],
   "source": [
    "!pip install 'azure-quantum[cirq]' --quiet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "e871031fa9c0"
   },
   "source": [
    "## Connecting to the Azure Quantum service\n",
    "\n",
    "To connect to the Azure Quantum service, find the resource ID and location of your Workspace from the Azure Quantum portal here: https://portal.azure.com. Navigate to your Azure Quantum workspace and copy the values from the header.\n",
    "\n",
    "<img src=\"azure-quantum-resource-id.png\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "414354c570ab"
   },
   "source": [
    "Paste the values into the `AzureQuantumService` constructor below to create a `service` that connects to your Azure Quantum Workspace. Optionally, specify a default target:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "ebebb7d4fdc9"
   },
   "outputs": [],
   "source": [
    "from azure.quantum.cirq import AzureQuantumService\n",
    "\n",
    "service = AzureQuantumService(resource_id=\"\", location=\"\", default_target=\"ionq.simulator\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "698aff542247"
   },
   "source": [
    "### List all IonQ targets\n",
    "\n",
    "You can now list all the targets that you have access to, including the current queue time and availability. To only return the IonQ provider's targets, you can specify the optional `provider_id` input argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "1cb20cd11fe2"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Target name=\"ionq.qpu\", avg. queue time=19 s, Available>,\n",
      " <Target name=\"ionq.simulator\", avg. queue time=0 s, Available>\n",
      "\n"
     ]
    }
   ],
   "source": [
    "service.targets(provider_id=\"ionq\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "85da3c1dc898"
   },
   "source": [
    "To read more about the Simulator and QPU specifications such as number of qubits, connectivity, system time scales and fidelities, you can check out the [IonQ Provider Reference](https://docs.microsoft.com/azure/quantum/provider-ionq)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "68cab2307e55"
   },
   "source": [
    "## Run a simple circuit\n",
    "\n",
    "Let's create a simple Cirq circuit to run. This circuit uses the square root of X gate, native to the IonQ hardware system."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "id": "a60b2ef3454c"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"overflow: auto; white-space: pre;\">0: ───X^0.5───@───M(&#x27;b&#x27;)───\n",
       "              │   │\n",
       "1: ───────────X───M────────</pre>"
      ],
      "text/plain": [
       "0: ───X^0.5───@───M('b')───\n",
       "              │   │\n",
       "1: ───────────X───M────────"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cirq\n",
    "\n",
    "q0, q1 = cirq.LineQubit.range(2)\n",
    "circuit = cirq.Circuit(\n",
    "    cirq.X(q0) ** 0.5,  # Square root of X\n",
    "    cirq.CX(q0, q1),  # CNOT\n",
    "    cirq.measure(q0, q1, key='b'),  # Measure both qubits\n",
    ")\n",
    "circuit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "53299a7e1758"
   },
   "source": [
    "You can now run the program via the Azure Quantum service and get the result. The following cell will submit a job that runs the circuit with 100 repetitions, wait until the job is completed and return the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "2d9308c360fc"
   },
   "outputs": [],
   "source": [
    "result = service.run(program=circuit, repetitions=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "14d1f6f41a91"
   },
   "source": [
    "This returns a `cirq.Result` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "5fda91a0f7a7"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b=0001101010110000111010001110010111010001101010001111100011100001111100101011101101001000001000011000, 0001101010110000111010001110010111010001101010001111100011100001111100101011101101001000001000011000\n"
     ]
    }
   ],
   "source": [
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ee14a47094f7"
   },
   "source": [
    "### Run on IonQ QPU\n",
    "\n",
    "#### Note: The time required to run a circuit on the QPU may vary depending on current queue times."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5120d982559e"
   },
   "source": [
    "The previous job ran on the default simulator you specified, `\"ionq.simulator\"`. To run on the QPU, provide `\"ionq.qpu\"` as the `target` argument:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "629315164a7e"
   },
   "outputs": [],
   "source": [
    "result = service.run(\n",
    "    program=circuit,\n",
    "    repetitions=100,\n",
    "    target=\"ionq.qpu\",\n",
    "    timeout_seconds=500,  # Set timeout to 500 seconds to accommodate current queue time on QPU\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "66ef6327353e"
   },
   "source": [
    "Again, this returns a `cirq.Result` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "9110b235e149"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b=0101011011011111100001011101101011011110100010000000011110111000100100110110101100110001001111101111, 0101011011011111100001011101101011011110100010000000011110111000100100110110101100110001001111101111\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "f83f66f10ed3"
   },
   "source": [
    "## Asynchronous model using Jobs\n",
    "\n",
    "For long-running circuits, it can be useful to run them asynchronously. The `service.create_job` method returns a `Job`, which you can use to get the results after the job has run successfully."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "9e88a0a99a3a"
   },
   "outputs": [],
   "source": [
    "job = service.create_job(program=circuit, repetitions=100, target=\"ionq.simulator\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ea5ac9e947bb"
   },
   "source": [
    "To check on the job status, use `job.status()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "c75961a4dfba"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ready'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "job.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4d3e8cb44f61"
   },
   "source": [
    "To wait for the job to be done and get the results, use the blocking call `job.results()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "id": "6d9dee6512cc"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "00: 0.5\n",
      "11: 0.5\n"
     ]
    }
   ],
   "source": [
    "result = job.results()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "27aefb43fd38"
   },
   "source": [
    "This returns a `SimulatorResult` object that contains the state probabilities to represent the output. For instance, in the example above, you measure either \"00\" or \"11\". To get the probabilities for each bit string, use the `.probabilities()` method. This returns a dictionary where the keys are the classical register values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "id": "7ba3c8c48520"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: 0.5, 3: 0.5}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.probabilities()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ca2a3626d731"
   },
   "source": [
    "To convert this to a `cirq.Result` object, use `result.to_cirq_result()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "id": "24908a9b4625"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b=1101000111010101101001011100001111101110011000100111101000010100101000001100010010011010111010011101, 1101000111010101101001011100001111101110011000100111101000010100101000001100010010011010111010011101"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.to_cirq_result()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "getting_started_ionq.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
